﻿using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using 基于WPF的电缆采样数据分析系统.MsgEvents;
using 基于WPF的电缆采样数据分析系统.MsgEvents.DeviceStatus;

namespace 基于WPF的电缆采样数据分析系统.Views
{
    /// <summary>
    /// MainWin.xaml 的交互逻辑
    /// </summary>
    public partial class MainWin : Window
    {

        /// <summary>
        /// 发布订阅
        /// </summary>
        private readonly IEventAggregator Aggregator;

        public MainWin()
        {
            Aggregator = App.GetService<IEventAggregator>();
            // 导航到报表分析区
            Aggregator.GetEvent<MsgNavicationTabulation>().Subscribe(AccountingReport);
            // 导航到历史数据区
            Aggregator.GetEvent<MsgNavicationHistoricalDataArea>().Subscribe(HistoricalDataArea);
            // 导航到系统设置区
            Aggregator.GetEvent<MsgNavicationSetting>().Subscribe(SettingsArea);
            // 设置用户别名
            Aggregator.GetEvent<MsgLoginUserName>().Subscribe(SetUserAlias);

            InitializeComponent();

            // 主界面默认为主页
            lbMenu.SelectedIndex = 0;
        }

        /// <summary>
        /// 左侧菜单选中项改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            drawerHost.IsLeftDrawerOpen = false;
        }

        /// <summary>
        /// 最小化按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMin_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        /// <summary>
        /// 最大化按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMax_Click(object sender, RoutedEventArgs e)
        {
            // 窗口状态正常，则让窗口最大化
            if (WindowState == WindowState.Normal)
            {
                WindowState = WindowState.Maximized;
                return;
            }

            // 窗口状态最大化，则让窗口正常
            if (WindowState == WindowState.Maximized)
            {
                WindowState = WindowState.Normal;
                return;
            }
        }

        /// <summary>
        /// 关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            //this.Close();
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }

        /// <summary>
        /// 头部双击鼠标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ColorZone_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            // 窗口状态正常，则让窗口最大化
            if (WindowState == WindowState.Normal)
            {
                WindowState = WindowState.Maximized;
                return;
            }

            // 窗口状态最大化，则让窗口正常
            if (WindowState == WindowState.Maximized)
            {
                WindowState = WindowState.Normal;
                return;
            }
        }

        /// <summary>
        /// 回到主页按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HomeBtn_Click(object sender, RoutedEventArgs e)
        {
            lbMenu.SelectedIndex = 0;
        }

        /// <summary>
        /// 导航到报表分析区
        /// </summary>
        public void AccountingReport()
        {
            lbMenu.SelectedIndex = 2;
        }

        /// <summary>
        /// 导航到历史数据区
        /// </summary>
        public void HistoricalDataArea()
        {
            lbMenu.SelectedIndex = 3;
        }

        /// <summary>
        /// 导航到系统设置区
        /// </summary>
        public void SettingsArea()
        {
            lbMenu.SelectedIndex = 4;
        }

        /// <summary>
        /// 设置用户别名
        /// </summary>
        /// <param name="loginName"></param>
        private void SetUserAlias(string loginName)
        {
            LoginName.Text = loginName;
        }
    }
}
